 /**
 * Description: 物资出库明细
 * @author:Hunter
 * @since:1.0.0
 *
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-04-08   Hunter       1.0.0     Initial
 */
package com.metro.paperless.app.service.impl;

import com.metro.core.dao.IMapper;
import com.metro.core.service.BaseServiceSupport;
import com.metro.core.service.ServiceException;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.metro.paperless.app.mapper.IAppOutDetailMapper;
import com.metro.paperless.app.service.inf.IAppOutDetailService;
import com.metro.paperless.biz.beans.vo.BizMaterialsManage;
import com.metro.paperless.biz.mapper.IBizMaterialsManageMapper;
import com.metro.paperless.app.beans.vo.AppOutDetail;

import java.util.List;

 /**
 * 物资出库明细<br>
 *
 * @author:Hunter
 * @since:1.0.0
 * @version [1.0.0, 2018-04-08]
 */
@Service("appoutdetailService")
public class AppOutDetailService extends BaseServiceSupport<AppOutDetail> implements IAppOutDetailService{
    //物资出库明细Mapper
    @Autowired
    private IAppOutDetailMapper appoutdetailMapper;
    
    @Autowired
    private IBizMaterialsManageMapper materialsManageMapper;

    @Override
    public IMapper getMapper() {
        return appoutdetailMapper;
    }

    @Override
    public IResult save(AppOutDetail entity) {
        try {
            appoutdetailMapper.save(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增物资出库明细失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult update(AppOutDetail entity) {
        try {
            appoutdetailMapper.update(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改物资出库明细失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult delete(Long id) {
        try {
            appoutdetailMapper.delete(id);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult deleteByIds(String ids) {
        try {
            appoutdetailMapper.deleteByIds(ids);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

     @Override
     public List<AppOutDetail> queryListByOutId(Long id) {
         List<AppOutDetail> outDetails = appoutdetailMapper.queryListByOutId(id);
         return outDetails;
     }

	@Override
	public IResult deleteInfoById(Long id) {
		try{
			AppOutDetail outDetail = appoutdetailMapper.getById(id);
			BizMaterialsManage materialsManager = materialsManageMapper.getById(outDetail.getMaterialsId());
			appoutdetailMapper.delete(id);
			materialsManager.setEnableStockNum(materialsManager.getEnableStockNum() + outDetail.getOutNum());
			materialsManageMapper.update(materialsManager);
			return ResultCode.SUCCESS;
		}catch(Exception e){
			log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
		}
	}
 }